package com.cmc6.pay.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.cmc6.common.util.ApiResult;
import com.cmc6.pay.config.AliPayProperties;
import com.cmc6.pay.model.dto.AliPayDTO;
import com.cmc6.pay.model.enums.TradeStatusEnum;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 支付宝，支付工具类
 */
@Component
public class AliPayUtil {

    public static AliPayProperties aliPayProperties;

    @Resource
    private void setAliPayProperties(AliPayProperties val) {
        AliPayUtil.aliPayProperties = val;
    }

    /**
     * 支付
     */
    @SneakyThrows
    public static String pay(AliPayDTO dto) {

        Assert.notBlank(dto.getOutTradeNo());
        Assert.notNull(dto.getTotalAmount());
        Assert.notBlank(dto.getSubject());
        int compare = DateUtil.compare(dto.getTimeExpire(), new Date());
        if (compare < 0) {
            ApiResult.error("操作失败：支付过期时间晚于当前时间");
        }

        AlipayClient alipayClient = new DefaultAlipayClient(aliPayProperties);

        AlipayTradePagePayRequest aliPayRequest = new AlipayTradePagePayRequest();
        aliPayRequest.setReturnUrl(aliPayProperties.getReturnUrl());
        aliPayRequest.setNotifyUrl(aliPayProperties.getNotifyUrl());

        JSONObject bizContent = JSONUtil.createObj();
        bizContent.set("out_trade_no", dto.getOutTradeNo());
        bizContent.set("total_amount", dto.getTotalAmount());
        bizContent.set("subject", dto.getSubject());
        bizContent.set("body", dto.getBody());
        bizContent.set("product_code", "FAST_INSTANT_TRADE_PAY");
        bizContent.set("time_expire", DateUtil.formatDateTime(dto.getTimeExpire()));

        aliPayRequest.setBizContent(bizContent.toString());

        AlipayTradePagePayResponse response = alipayClient.pageExecute(aliPayRequest);
        if (!response.isSuccess()) {
            ApiResult.error("支付宝支付失败：" + response.getSubMsg());
        }

        return response.getBody();
    }

    /**
     * 交易查询接口
     *
     * @param outTradeNo 商户订单号，商户网站订单系统中唯一订单号，必填
     */
    @SneakyThrows
    public static TradeStatusEnum query(String outTradeNo) {

        Assert.notBlank(outTradeNo);

        AlipayClient alipayClient = new DefaultAlipayClient(aliPayProperties);

        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();

        JSONObject bizContent = new JSONObject();
        bizContent.set("out_trade_no", outTradeNo);

        request.setBizContent(bizContent.toString());

        AlipayTradeQueryResponse response = alipayClient.execute(request);
        if (!response.isSuccess()) {
            ApiResult.error("支付宝查询失败：" + response.getSubMsg());
        }

        return TradeStatusEnum.getByAliPayCode(response.getTradeStatus());

    }

}
